package com.amazon.trietree.io;

import com.amazon.trietree.dedupstruct.MemoryTree;
import com.amazon.trietree.fingerprint.FingerPrint;
import com.amazon.trietree.fingerprint.FingerPrintFileContainer;

import java.io.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Tylaar
 * Date: 10/30/12
 * Time: 4:10 PM
 * Company: Amazon.com
 */
public class FingerPrintFileIOImpl implements FingerPrintIOInterface {
    private BufferedReader bufferedReader;
    private FileWriter writer;
    private MemoryTree tree;
    private String fileName;

    public FingerPrintFileIOImpl(String fileName) throws FileNotFoundException {
        this.fileName = fileName;
        bufferedReader = new BufferedReader(new FileReader(this.fileName));
        writer = null;
        tree = null;
    }

    public BufferedReader getBufferedReader() {
        return bufferedReader;
    }

    public void setBufferedReader(BufferedReader bufferedReader) {
        this.bufferedReader = bufferedReader;
    }

    public FileWriter getWriter() {
        return writer;
    }

    public void setWriter(FileWriter writer) {
        this.writer = writer;
    }

    public MemoryTree getTree() {
        return tree;
    }

    public void setTree(MemoryTree tree) {
        this.tree = tree;
    }

    //private FingerPrintFileDecoder decoder;

    @Override
    public List<FingerPrint> readFingerPrintFromStream() {
        try {
            FingerPrintFileContainer container;
            String intermediaLine = bufferedReader.readLine();
            while(intermediaLine != null){
                container = FingerPrintFileDecoder.decodeLine(intermediaLine);
                intermediaLine = bufferedReader.readLine();
            }
            return null;
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public FingerPrintFileContainer readSingleTrackFromStream(){

        String intermediaLine = null;
        try {
            intermediaLine = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if(intermediaLine != null)
            return FingerPrintFileDecoder.decodeLine(intermediaLine);
        else
            return null;
    }

    @Override
    public boolean appendToFile(List<FingerPrint> fpList) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean insertToTrieTree(List<FingerPrint> fpList) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
